package misc;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Properties implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private int interval = 35;
	private int sequenceLenght = 4;
	private int checkSum = 0;
	private CharacterValue characterValue = null;

	public Properties(){
		this.interval = 0;
		this.sequenceLenght = 0;
		this.checkSum = 0;
		this.characterValue = loadCharValue();
	}

	public Properties(int interval, int sequenceLenght, int checkSum){
		this.interval = interval;
		this.sequenceLenght = sequenceLenght;
		this.checkSum = checkSum;
		this.characterValue = loadCharValue();
	}

	public Properties(int interval, int sequenceLenght){
		this.interval = interval;
		this.sequenceLenght = sequenceLenght;
		this.checkSum = ChecksumRule.defaultRule(interval, sequenceLenght);
		this.characterValue = loadCharValue();
	}

	public void setCharValue(char character, int value){
		this.characterValue.setChar(character, value);
		saveCharValue();
	}

	private void saveCharValue(){
		if (new File("charval.val").exists()){
			new File("charval.val").delete();
		}
			try {
				ObjectOutputStream out = new ObjectOutputStream(
						new BufferedOutputStream(
								new FileOutputStream("charval.val")));
				out.writeObject(characterValue);
				out.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
	}

	private CharacterValue loadCharValue(){
		if (new File("charval.val").exists())
		{
			try {
				ObjectInputStream in = new ObjectInputStream(
						new BufferedInputStream(
								new FileInputStream("charval.val")));
				return (CharacterValue)in.readObject();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		return new CharacterValue();
	}

	public CharacterValue getCharacterValue() {
		return characterValue;
	}

	public int getInterval() {
		return interval;
	}
	public int getSequenceLenght() {
		return sequenceLenght;
	}
	public int getcheckSum() {
		return checkSum;
	}
}
